<?php

/**
 * Lostpassword Form : keep user_login or email of user
 * called by : loginController
 */
class LostPasswordForm extends CFormModel
{
	//public $userLogin;
	public $useremail;
	private $_identity;

	/**
	 * Declares the validation rules.
	 * The rules state that userLogin or userEmail is required
	 * 
	 */
	public function rules()
	{
		return array(
			// username and password are required
			array('useremail', 'required'),
			array('useremail','authenticate'),
			
		);
	}

	/**
	 * Authenticates the password.
	 * This is the 'authenticate' validator as declared in rules().
	 */
	public function authenticate($attribute,$params)
	{
		if(!$this->hasErrors())
		{
			 Yii::log('***->authenticate in LostPasswordForm  in Model');
			$account= User::model()-> findByAttributes(array('user_email'=> $this->useremail));
			if($account == NULL) $this->addError('useremail','This email doesn\'t exist.');
			else{
			 //$account=User::model()->findByPk($account_id);
			  $this->_identity=new UserIdentity($account->user_name,$account->user_password);
			 $this->_identity->useremail=$this->useremail;
			 $this->_identity->setId($account->user_id);
			 $this->_identity->errorCode = UserIdentity::ERROR_NONE;
			} 
			 
		}
	}

	/**
	 * 
	 */
	public function lostPassword()
	{
		if($this->_identity===null)
		{
			 Yii::log (" *** truong hop ko authentication ?");
			 $account= User::model()-> findByAttributes(array('user_email'=> $this->useremail));
			$this->_identity=new UserIdentity($account->user_name,$account->user_password);
			$this->_identity->emailAuthenticate();
		}
		Yii::log('*** -> truoc khi check code');
		if($this->_identity->errorCode === UserIdentity::ERROR_NONE)
		{ 
			 Yii::log(' *** -> vao phan gui email');
			     $this->_identity->password = $this->randomPassword();
			    //input send email code here
				 $body = "Your account :<br/> username: ".$this->_identity->username;
				 $body = "<br/> password: ".$this->_identity->password;
				  $headers="From: {Yii:app()->params['adminEmail']}\r\Reply-To:noreply";
				  $check = mail($this->_identity->useremail,"Your new password at {Yii:app()->name} has been reseted",
				   $body,$headers);
				  if($check ==1) {
				  	Yii::log('**** gui email thanh cong toi :'.$this->_identity->getId()."pass:".$this->_identity->password);
					
				  }
				 else{ 
 				  Yii::log ('*** gui email fail toi:'.$this->_identity->useremail); 
				 return false;
				 }
				User::model()->updateByPk($this->_identity->getId(), array('user_password'=> md5($this->_identity->password)));

			return true;
		}
		else
			return false;
	}
	public function randomPassword(){
	   // code random password here
	    $codelenght = 6;
	    $newcode_length=0;
	    $newcode= NULL;
	while($newcode_length < $codelenght) {
		$x=1;
		$y=3;
		$part = rand($x,$y);
		if($part==1){$a=48;$b=57;}  // Numbers
		if($part==2){$a=65;$b=90;}  // UpperCase
		if($part==3){$a=97;$b=122;} // LowerCase
		$code_part=chr(rand($a,$b));
		$newcode_length = $newcode_length + 1;
		$newcode = $newcode.$code_part;
		}
		return $newcode;


	}
}
